<template>
  <div class="login">
    <div class="login_context">
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" class="login_box" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" maxlength="18" prefix-icon="el-icon-user" show-word-limit></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password" autocomplete="off" prefix-icon="el-icon-lock"
                    show-password="show-password" type="password"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login1()">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>


</template>

<script>
export default {
  name: "login",
  data() {
    return {
      labelPosition: 'right',
      role: null,
      ruleForm: {
        username: '',
        password: '',
        sid: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 1, max: 18, message: '长度在 1 到 18 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 1, max: 18, message: '长度在 1 到 18 个字符', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    resetForm(formName) {
      // 全部置空
      this.$refs[formName].resetFields();
    },
    login1() {
      this.role = "admin";
      this.login3();
    },
    login2() {
      this.role = "user";
      this.login3();
    },
    login3() {
      const _this = this;
      if (this.ruleForm.username == '' || this.ruleForm.password == '') {
        this.$alert('请输入用户名和密码再选择登录！', '登录失败', {
          confirmButtonText: '确定',
          callback: action => {
            this.$message({
              type: 'info',
              message: '请输入用户名和密码！'
            });
          }
        });
      }
      axios.get('http://localhost:8000/std/login/' + this.ruleForm.username + '/' + this.ruleForm.password + '').then(function (resp) {
        console.log(resp);
        if (resp.data.id==null) {
          _this.$alert('用户名或密码错误！', '登录失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.$message({
                type: 'info',
                message: '请输入正确用户名和密码！'
              });
            }
          });
        } else {
          if (resp.data.role == "admin") {
            _this.$router.push('/student')
          } else {
            _this.Global.sid = resp.data.sid;
            _this.$router.push('/personal');
          }
        }
      })
    }
  }
}
</script>

<style scoped="scoped">
.login{
  background: url("../assets/login_bg.jpeg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
  top: 0px;
  left: 0px;
}

.login_context {
  width: 450px;
  height: 300px;
  background: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 10px;

}

.login_logo {
  width: 150px;
  height: 150px;
  position: absolute;
  top: -75px;
  left: 50%;
  margin-left: -75px;
  border: 1px solid #eee;
  border-radius: 50%;
  background: #fff;
  padding: 10px;
  box-shadow: 0 0 3px 2px #fff;
}

.login_logo img {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: rgb(238, 238, 238);
}

.login_box {
  width: 100%;
  position: absolute;
  bottom: 0;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>
